package com.ruleengine.db.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.ruleengine.db.service.RuleService;
import com.ruleengine.db.mapper.RuleMapper;
import com.ruleengine.db.model.Rule;

/**
 * @author lianghanwei on 2024/3/17
 */
@Service
public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements RuleService {

    private final RuleMapper ruleMapper;

    public RuleServiceImpl(RuleMapper ruleMapper) {
        this.ruleMapper = ruleMapper;
    }

    @Override
    public List<Rule> getRules(String tenantId, String rulechainId) {
        return ChainWrappers.lambdaQueryChain(ruleMapper)
            .eq(Rule::getTenantId, tenantId)
            .eq(Rule::getRulechainId, rulechainId)
            .list();
    }

    @Override
    public boolean softDeleteRulesByRulechainId(String tenantId, String rulechainId) {
        return ChainWrappers.lambdaUpdateChain(ruleMapper)
            .eq(Rule::getTenantId, tenantId)
            .eq(Rule::getRulechainId, rulechainId)
            .remove();
    }
}
